<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="80px">
      <el-form-item label="标签名称" prop="title">
        <el-input v-model="dataForm.title" placeholder="标签名称"></el-input>
      </el-form-item>
      <el-form-item label="标签颜色" prop="colorCode">
        <el-color-picker v-model="dataForm.colorCode" show-alpha :predefine="predefineColors">
        </el-color-picker>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      color: 'rgba(255, 69, 0, 0.68)',
      predefineColors: [
        '#ff4500',
        '#ff8c00',
        '#ffd700',
        '#90ee90',
        '#00ced1',
        '#1e90ff',
        '#c71585',
        'rgba(255, 69, 0, 0.68)',
        'rgb(255, 120, 0)',
        'hsv(51, 100, 98)',
        'hsva(120, 40, 94, 0.5)',
        'hsl(181, 100%, 37%)',
        'hsla(209, 100%, 56%, 0.73)',
        '#c7158577'
      ],
      visible: false,
      dataForm: {
        id: 0,
        title: '',
        colorCode: '',
        codeTitle: '',
        createdAt: '',
        createdBy: '',
        modifiedAt: '',
        modifiedBy: '',
        isDeleted: ''
      },
      dataRule: {
        title: [
          { required: true, message: '标签名称不能为空', trigger: 'blur' }
        ],
        colorCode: [
          { required: true, message: '标签颜色code(使用RGB表示法，如:#000)不能为空', trigger: 'blur' }
        ],
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/film/filmlabel/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.title = data.filmLabel.title
              this.dataForm.colorCode = data.filmLabel.colorCode
              this.dataForm.codeTitle = data.filmLabel.codeTitle
              this.dataForm.createdAt = data.filmLabel.createdAt
              this.dataForm.createdBy = data.filmLabel.createdBy
              this.dataForm.modifiedAt = data.filmLabel.modifiedAt
              this.dataForm.modifiedBy = data.filmLabel.modifiedBy
              this.dataForm.isDeleted = data.filmLabel.isDeleted
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/film/filmlabel/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'title': this.dataForm.title,
              'colorCode': this.dataForm.colorCode,
              'codeTitle': this.dataForm.codeTitle,
              'createdAt': this.dataForm.createdAt,
              'createdBy': this.dataForm.createdBy,
              'modifiedAt': this.dataForm.modifiedAt,
              'modifiedBy': this.dataForm.modifiedBy,
              'isDeleted': this.dataForm.isDeleted
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
